package com.example.filedemo;

import android.util.Log;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class SaveFileJava {
    //存文件
    Myapplication ma = new Myapplication();
    void save(String text) throws IOException {
        BufferedWriter writer = null;
        FileOutputStream output = null;

        try {
            ma.onCreate();
            Log.e("cxp", "savepath is: " + ma.filepath + File.separator + "data.txt" );
            File file = new File(ma.filepath + File.separator + "data.txt");
            output = new FileOutputStream(file);
            writer = new BufferedWriter(new OutputStreamWriter(output));
            writer.write(text);
            Log.e("SaveFileJava", "success save: " );
        } catch (FileNotFoundException e) {
            Log.e("SaveFileJava", "FileNotFoundException save: " + "failed" );
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("SaveFileJava", "IOException save: " + "failed" );
            e.printStackTrace();
        }finally {
            if(writer != null){
                writer.close();
            }
            if(output != null){
                output.close();
            }
        }
    }
    //创建文件夹
    void createDir(String dirName){
        File file = new File(ma.filepath + File.separator + dirName);
        if(!file.exists()){
            file.mkdirs();
        }
    }
}
